package com.bidevalution.business.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import com.qtp.core.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.bidevalution.business.service.BeSectionMethodService;
import com.bidevalution.dao.entity.BeMethodDefine;
import com.bidevalution.dao.entity.BeSectionInfo;
import com.bidevalution.dao.entity.BeSectionMethod;
import com.bidevalution.dao.mapper.BeMethodDefineMapper;
import com.bidevalution.dao.mapper.BeSectionMethodMapper;
import com.googlecode.aviator.AviatorEvaluator;
import com.qtp.core.util.IdsUtil;
import org.springframework.transaction.annotation.Transactional;

/**
 * 评标方法配置
 * @author daizhifeng
 * @date 2019-08-06
 */
@Service
public class BeSectionMethodServiceImpl implements BeSectionMethodService {
	@Autowired
	private BeSectionMethodMapper beSectionMethodMapper;
	@Autowired
	private BeMethodDefineMapper beMethodDefineMapper;
	@Override
	public BeSectionMethod save(BeSectionInfo beSectionInfo, String methodCode, String isBidHide,String projArea) {
		BeMethodDefine beMethodDefine = beMethodDefineMapper.selectByCode(methodCode,projArea);
		if(beMethodDefine == null) {
			return null;
		}

		BeSectionMethod beSectionMethod = beSectionMethodMapper.selectByPrimaryKey(beSectionInfo.getSeqId());
		if(beSectionMethod == null) {
			beSectionMethod = new BeSectionMethod();
			beSectionMethod.setCreateTime(new Date());
			beSectionMethod.setIsBidHide(Integer.valueOf(isBidHide));
			beSectionMethod.setMethodId(beMethodDefine.getSeqId());
			beSectionMethod.setMethodCode(methodCode);
			beSectionMethod.setMethodName(beMethodDefine.getName());
			beSectionMethod.setSectionId(beSectionInfo.getSeqId());
			beSectionMethod.setTemplateConfig(beMethodDefine.getTemplateConfig());
			beSectionMethod.setUpdateTime(new Date());
			beSectionMethod.setSubmitCode(IdsUtil.uuid().replaceAll("-", ""));
			beSectionMethodMapper.insert(beSectionMethod);
			
		}else {
			beSectionMethod.setCreateTime(new Date());
			beSectionMethod.setIsBidHide(Integer.valueOf(isBidHide));
			beSectionMethod.setMethodId(beMethodDefine.getSeqId());
			beSectionMethod.setMethodCode(methodCode);
			beSectionMethod.setMethodName(beMethodDefine.getName());
			beSectionMethod.setSectionId(beSectionInfo.getSeqId());
			beSectionMethod.setTemplateConfig(beMethodDefine.getTemplateConfig());
			beSectionMethod.setUpdateTime(new Date());
			beSectionMethodMapper.updateByPrimaryKey(beSectionMethod);
		}
	
		return beSectionMethod;
	}
	@Override
	public BeSectionMethod selectBySectionId(String sectionId) {
		// TODO Auto-generated method stub
		return beSectionMethodMapper.selectByPrimaryKey(sectionId);
	}
	@Override
	public int updateByPrimaryKey(BeSectionMethod record) {
		// TODO Auto-generated method stub
		return beSectionMethodMapper.updateByPrimaryKey(record);
	}

	@Override
	@Transactional
	public void updateByPrimaryKeySelective(String sectionId, String submitCode) throws Exception {
		BeSectionMethod beSectionMethod = new BeSectionMethod();
		beSectionMethod.setSectionId(sectionId);
		beSectionMethod.setSubmitCode(submitCode);
		beSectionMethod.setUpdateTime(DateUtil.now());
		if (beSectionMethodMapper.updateByPrimaryKeySelective(beSectionMethod) < 1) {
			throw new Exception("*****更新submitCode失败*****");
		}
	}


}
